﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.IO;
using ReporterApplication.Expressions;
using System.Linq.Expressions;


namespace ReporterApplication
{
    public partial class FormTest : Form
    {
        public FormTest()
        {
            InitializeComponent();
        }

        private void Testbutton_Click(object sender, EventArgs e)
        {
            XDocument xd = XDocument.Load("D:\\ProvisionReports\\Result\\CIF.xml", LoadOptions.SetLineInfo);
            var elements = xd.Element("cif").Element("notebook").Element("instances")
                .Elements().Where(x => x.Attribute("setLabel") != null);

            //.Select(x => x.Attribute("setLabel").Value);

            var sb = new StringBuilder();

            int counter = 0;

            Dictionary<string, XElement> del = new Dictionary<string, XElement>();

            Dictionary<string, List<XElement>> refel = new Dictionary<string, List<XElement>>();


            var idels = xd.Descendants().Where(x => x.Attribute("id") != null);
            foreach (var idel in idels)
            {
                del.Add(idel.Attribute("id").Value, idel);
            }

            var refels = xd.Descendants().Where(x => x.Attribute("refid") != null);
            foreach (var idel in refels)
            {
                var refid = idel.Attribute("refid").Value;
                List<XElement> lis = null;

                if (!refel.ContainsKey(refid))
                {
                    lis = new List<XElement>();
                    refel.Add(refid, lis);
                }
                else
                    lis = refel[refid];


                lis.Add(idel);
            }

            Structure1(elements, sb, del, refel);

            textBox1.Text = sb.ToString(); ;
        }

        private static void Structure1(IEnumerable<XElement> elements, StringBuilder sb, Dictionary<string, XElement> del, Dictionary<string, List<XElement>> refel)
        {
            foreach (var item in elements)
            {
                var label = item.Attribute("setLabel").Value.ToLower();
                if (item.Attribute("setLabel").Value.ToLower() == "workflow models")
                {
                    sb.Append(item.Attribute("setLabel").Value);
                    sb.Append("\r\n");

                    foreach (var innerItem in item.Elements())
                    {
                        sb.Append("\t");
                        sb.Append(innerItem.Attribute("name").Value);
                        sb.Append("\r\n");

                        var id = innerItem.Attribute("id").Value;

                        foreach (var linkItem in innerItem.Elements().Where(x => x.Attribute("refid") != null))
                        {
                            sb.Append("\t\t");
                            sb.Append(linkItem.Name);
                            sb.Append("\r\n");

                            var refid = linkItem.Attribute("refid").Value;

                            if (del.ContainsKey(refid))
                            {

                                var refElem = del[refid];


                                sb.Append("\t\t\t");
                                sb.Append(refElem.Name + " $ " + refElem.Attribute("name").Value);
                                sb.Append("\r\n");
                            }


                        }

                        if (refel.ContainsKey(id))
                        {
                            sb.Append("\t\tUsed By:");
                            sb.Append("\r\n");

                            foreach (var derel in refel[id])
                            {
                                if (derel.Attribute("name") != null)
                                {
                                    sb.Append("\t\t\t");
                                    sb.Append(derel.Name + " $ " + derel.Attribute("name").Value);
                                    sb.Append("\r\n");
                                }

                                var parent = derel.Parent;

                                if (parent.Attribute("name") != null)
                                {
                                    sb.Append("\t\t\t");
                                    sb.Append(derel.Name + " : " + "(" + parent.Name + ") $ " + parent.Attribute("name").Value);
                                    sb.Append("\r\n");
                                }
                            }
                        }
                    }
                }
            }
        }

        private void testSectionbutton_Click(object sender, EventArgs e)
        {
            ReportEngine re = new ReportEngine();
            re.Report(new ReportSettings
            {
                CifFolder = "D:\\ProvisionReports\\Result\\CIF.xml",
                TemplateFile = "text.docx",
                ResultFile = "text_result.docx"
            });
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBar1.Value = XmlCIF.Progress;
        }

        private void parserbutton_Click(object sender, EventArgs e)
        {
            
        }

        
    }
}
